package com.thesis.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.DocumentInfo;
import com.thesis.manage.domain.Student;
import com.thesis.manage.service.DocumentInfoService;
import com.thesis.manage.service.StudentThesisService;
import com.thesis.manage.service.ThesisService;
import com.thesis.manage.service.model.AnswerStateEnum;
import com.thesis.manage.service.model.DocumentEnum;
import com.thesis.manage.service.model.TimeTypeEnum;
import com.thesis.manage.service.module.bean.*;
import com.thesis.manage.service.module.vo.StudentThesisVO;
import com.thesis.manage.service.module.vo.TimeVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author php
 * @date 2018/3/28
 */
@RequestMapping("/document")
@RestController
public class DocumentController extends BaseController {
    @Resource
    private DocumentInfoService documentInfoService;
    @Resource
    private StudentThesisService studentThesisService;
    @Resource
    private ThesisService thesisService;

    /**
     * 查看自己的任务书
     */
    @GetMapping("/taskBook")
    public Map<String, Object> findTaskBook() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findByStudentId(student.getId(), DocumentEnum.TASK_BOOK.getValue());
        });
    }

    /**
     * 保存开题报告
     *
     * @param openReport 开题报告
     */
    @PostMapping("/openReport/save")
    public Map<String, Object> saveOpenReport(@RequestBody OpenReport openReport) {
        return buildMessage(() -> {
            //时间验证
            Result time = thesisService.checkOpenApplication(TimeTypeEnum.APPLY.getValue());
            TimeVO timeVO = (TimeVO) time.getData();
            if (timeVO.getType().intValue() == AnswerStateEnum.NO_OPEN.getValue()) {
                throw new CommonException("no open", "开题报告未开启");
            }
            if (timeVO.getType().intValue() == AnswerStateEnum.END.getValue()) {
                throw new CommonException("end", "开题报告已结束");
            }
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            documentInfoService.saveDocument(JSONObject.toJSONString(openReport), DocumentEnum.OPEN_REPORT.getValue(), student.getId());
            return null;
        });
    }

    /**
     * 查询开题报告
     */
    @GetMapping("/report/info")
    public Map<String, Object> findOpenReport() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findOpenReportByStudentId(student.getId(), DocumentEnum.OPEN_REPORT.getValue());
        });
    }

    /**
     * 查询旧的开题报告
     */
    @GetMapping("/old/report")
    public Map<String, Object> findOldOpenReport() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findOldDocumentByStudentId(student.getId(), DocumentEnum.OPEN_REPORT.getValue());
        });
    }

    /**
     * 查询旧的开题报告（id）
     *
     * @param id id
     */
    @GetMapping("/{id}/old/report")
    public Map<String, Object> findOldOpenReportById(@PathVariable Long id) {
        return buildMessage(() -> documentInfoService.findOldDocumentById(id, DocumentEnum.OPEN_REPORT.getValue()));
    }

    /**
     * 保存中期检查
     *
     * @param midtermExamination 中期检查文本
     */
    @PostMapping("/midterm/save")
    public Map<String, Object> saveMidtermExamination(@RequestBody MidtermExamination midtermExamination) {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            documentInfoService.saveDocument(JSONObject.toJSONString(midtermExamination), DocumentEnum.MID_TERM_EXAMINATION.getValue(), student.getId());
            return null;
        });
    }

    /**
     * 查找中期检查
     */
    @GetMapping("/midterm/info")
    public Map<String, Object> findMidtermExamination() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findOpenReportByStudentId(student.getId(), DocumentEnum.MID_TERM_EXAMINATION.getValue());
        });
    }

    /**
     * 保存指导记录
     *
     * @param guideRecord 指导记录
     */
    @PostMapping("/guide/save")
    public Map<String, Object> saveGuideRecord(@RequestBody GuideRecord guideRecord) {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            documentInfoService.saveDocument(JSONObject.toJSONString(guideRecord), DocumentEnum.GUIDE_RECORD.getValue(), student.getId());
            return null;
        });
    }

    /**
     * 查找指导记录
     */
    @GetMapping("/guide/info")
    public Map<String, Object> findGuideRecord() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findOpenReportByStudentId(student.getId(), DocumentEnum.GUIDE_RECORD.getValue());
        });
    }

    /**
     * 保存外文译文和原件
     *
     * @param foreignLanguage 外文译文和原件
     */
    @PostMapping("/foreign/save")
    public Map<String, Object> saveForeignLanguage(@RequestBody ForeignLanguage foreignLanguage) {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            documentInfoService.saveDocument(JSONObject.toJSONString(foreignLanguage), DocumentEnum.FOREIGN_LANGUAGE.getValue(), student.getId());
            return null;
        });
    }

    /**
     * 查找外文译文和原件
     */
    @GetMapping("/foreign/info")
    public Map<String, Object> findForeignLanguage() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findOpenReportByStudentId(student.getId(), DocumentEnum.FOREIGN_LANGUAGE.getValue());
        });
    }

    /**
     * 保存文献综述
     *
     * @param literature 文献综述
     */
    @PostMapping("/literature/save")
    public Map<String, Object> saveLiterature(@RequestBody Literature literature) {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            documentInfoService.saveDocument(JSONObject.toJSONString(literature), DocumentEnum.LITERATURE.getValue(), student.getId());
            return null;
        });
    }

    /**
     * 查找文献综述
     */
    @GetMapping("/literature/info")
    public Map<String, Object> findLiterature() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findOpenReportByStudentId(student.getId(), DocumentEnum.LITERATURE.getValue());
        });
    }

    /**
     * 上传论文
     *
     * @param thesisInfo 上传论文
     */
    @PostMapping("/thesis/save")
    public Map<String, Object> saveThesisInfo(@RequestBody ThesisInfo thesisInfo) {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            thesisInfo.setAuthName(student.getName());
            StudentThesisVO thesis = this.studentThesisService.findByStudentId(student.getId());
            thesisInfo.setThesisName(thesis.getThesisName());
            thesisInfo.setDate(new Date());
            documentInfoService.saveDocument(JSONObject.toJSONString(thesisInfo), DocumentEnum.THESIS_INFO.getValue(), student.getId());
            return null;
        });
    }

    /**
     * 查找论文信息
     */
    @GetMapping("/thesis/info")
    public Map<String, Object> findThesisInfos() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findThesisInfos(student.getId());
        });
    }

    /**
     * 通过文档查询审核文档
     *
     * @param id id
     */
    @GetMapping("/{id}")
    public Map<String, Object> findDoucmentByDoucmentId(@PathVariable Long id) {
        return buildMessage(() -> {
            DocumentInfo documentInfo = new DocumentInfo();
            documentInfo.setDocumentId(id);
            return documentInfoService.findByOne(documentInfo);
        });
    }

    /**
     * 查看答辩安排
     */
    @GetMapping("/defence/arrange")
    public Map<String, Object> findDefenceArrange() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            return documentInfoService.findDefenceArrange(student.getCollegeName());
        });

    }
}
